﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.APP.Filters;

namespace Unmanned_MSUI.Areas.APP.Controllers
{
    [Area("APP")]
    [UserFilter]
    public class UserBalanceInfoController : Controller
    {
        private IUserBalanceInfoBLL _userBalanceInfoBLL;
        private IUserInfoBLL _userInfoBLL;
        public UserBalanceInfoController(
            IUserBalanceInfoBLL userBalanceInfoBLL
            ,IUserInfoBLL userInfoBLL
            )
        {
            _userBalanceInfoBLL = userBalanceInfoBLL;
            _userInfoBLL = userInfoBLL;
        }
       
        public IActionResult ListView_1()
        {
            return View();
        }
       
        public IActionResult CreateUserBalanceInfoView_1()
        {
            return View();
        }
        public IActionResult UpdateUserBalanceInfoView_1()
        {
            return View();
        }
        public IActionResult UpdateUserBalanceInfoView()
        {
            return View();
        }
        #region 返回用户姓名数据（GetSelectOptions）
        [HttpGet]
        public IActionResult GetSelectOptions()
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用BLL方法
            var data = _userBalanceInfoBLL.GetSelectOptions();

            //构建返回结果进行返回
            result.Code = 200;
            result.Msg = "获取成功!";
            result.IsSuccess = true;
            result.Data = data;
            return Json(result);

        }
        #endregion

        #region 获取用户余额列表的方法(GetUserBalanceInfos)
        [HttpGet]
        public IActionResult GetUserBalanceInfos(int page, int limit, string account, string userName)
        {
            //声明返回对象
            ReturnResult result = new ReturnResult();
            var userId = HttpContext.Request.Cookies["UserId"];
            int count;
            //调用BLL的获取用户方法
            var list = _userBalanceInfoBLL.GetUserBalanceInfos(page, limit, userId, account, userName, out count);
            //返回值
            result.Code = 0;
            result.IsSuccess = true;
            result.Msg = "获取用户余额列表成功";
            result.Count = count;
            result.Data = list;
            //返回结果
            return Json(result);
        }
        #endregion

        #region 获取用户余额列表的方法(GetUserBalances)
        [HttpGet]
        public IActionResult GetUserBalances(string id, string account, string userId, string userName)
        {
            //声明返回对象
            ReturnResult result = new ReturnResult();

            int count;
            
            //调用BLL的获取用户方法
            var list = _userBalanceInfoBLL.GetUserBalances(id, account, userId, userName, out count);
            //var userId = HttpContext.Request.Cookies["UserId"];
            //userbalanceInfo.UserId = userId;
            //返回值
            result.Code = 0;
            result.IsSuccess = true;
            result.Msg = "获取用户余额列表成功";
            result.Count = count;
            result.Data = list;
            //返回结果
            return Json(result);
        }
        #endregion

        #region 根据id获取用户余额详情(GetUserBalanceInfoById)
        /// <summary>
        /// 根据id获取用户余额详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserBalanceInfoById(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //判断id是否为空
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空";
                return Json(result);
            }
            //根据id查询数据信息，并判断是否为空
            UserBalanceInfo userBalanceInfo = _userBalanceInfoBLL.GetUserBalanceInfoById(id);
            if (userBalanceInfo == null)
            {
                result.Msg = "未获取用户余额信息";
            }
            else
            {
                result.Code = 200;
                result.Msg = "查询成功!";

                result.Data = new
                {
                    userBalanceInfo,//返回耗材类别信息
                };

            }
            return Json(result);//返回结果
        }
        #endregion

        #region 新增用户余额的方法(CreateUserBalanceInfo)
        [HttpPost]
        public IActionResult CreateUserBalanceInfo([FromForm] UserBalanceInfo userBalanceInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加的方法
            string msg;
            result.IsSuccess = _userBalanceInfoBLL.CreateUserBalanceInfo(userBalanceInfo, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }
        #endregion



        #region 编辑用户余额的方法（UpdateUserBalanceInfo）

        [HttpPost]
        public IActionResult UpdateUserBalanceInfo([FromForm] UserBalanceInfo userbalanceInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            var userId = HttpContext.Request.Cookies["UserId"];
            userbalanceInfo.UserId = userId;
            //调用方法
            string msg;
            bool isOk = _userBalanceInfoBLL.UpdateUserBalanceInfo(userbalanceInfo, out msg);
            result.Msg = msg;
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);

        }

        #endregion

        #region 充值提现（UpdateUserBalance）

        [HttpPost]
        public IActionResult UpdateUserBalance([FromForm] UserBalanceInfo userbalanceInfo,int balance)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            var userId = HttpContext.Request.Cookies["UserId"];
            userbalanceInfo.UserId = userId;
            //调用方法
            string msg;
            bool isOk = _userBalanceInfoBLL.UpdateUserBalance(userbalanceInfo, balance, out msg);
            result.Msg = msg;
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);

        }

        #endregion

    }
}
